//
//  ResetPasswordViewController.m
//  NewApp
//
//  Created by  on 12-6-29.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ResetPasswordViewController.h"
#import "NAAccount.h"
#import "PathHelper.h"
#import "ChecksEndViewController.h"

@interface ResetPasswordViewController ()
/**
 *  Reset password request did finished
 */
- (void) resetPasswordRequestDidFinished:(RequestEngine *) engine withObject:(id) object;
@end

@implementation ResetPasswordViewController

@synthesize scrollView,oldPasswordTextfield;
@synthesize newerPasswordTextfield,confirmPasswordTextfield;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //self.textView.autoresizingMask = UIViewAutoresizingNone;
    self.scrollView.contentSize =  CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height);
    
    [self.engine resetPasswordPageInfo];
}

- (void) dealloc {
    [resetPasswordReq setDelegate:nil];
    [resetPasswordReq release];
    [super dealloc];
}

#pragma mark -
#pragma mark - UITextfieldDelegate methods

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    
    BOOL canRetrun = (textField.text.length > 0) ? YES : NO;
    if (canRetrun) {
        [textField resignFirstResponder];
        self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        
        CGPoint offset = self.scrollView.contentOffset;
        offset.y = 0.f;
        
        [self.scrollView setContentOffset:offset animated:NO];
    }
    
    return canRetrun;
}

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height + 216.f);
    
    CGPoint offset = self.scrollView.contentOffset;
    offset.y = textField.frame.origin.y - 30.f;
    
    [self.scrollView setContentOffset:offset animated:YES];
}

#pragma mark -
#pragma mark - Override methods

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dict = (NSDictionary *) object;
        [dict retain];
        
        NSString *title = [dict objectForKey:@"top_title"];
        if (title != nil && title.length > 0)
            self.titleLabel.text = title;
        
        [dict release];
        
    }
}

#pragma mark - 
#pragma mark - Event handlers

- (IBAction) resetPasswordButtonPressed:(id) sender {
    if(self.oldPasswordTextfield.text.length == 0) {
        [NAError showAlertMessageWithTitle:@"" message:@"請輸入舊密碼"];
        return;
    }
    
    if(self.newerPasswordTextfield.text.length == 0) {
        [NAError showAlertMessageWithTitle:@"" message:@"請輸入新密碼"];
        return;
    }
    
    if(self.confirmPasswordTextfield.text.length == 0) {
        [NAError showAlertMessageWithTitle:@"" message:@"請確認新密碼"];
        return;
    }
    
    if (![self.confirmPasswordTextfield.text isEqualToString:self.newerPasswordTextfield.text]) {
        [NAError showAlertMessageWithTitle:@"" 
                                   message:@"倆次輸入的新密碼不一致"];
        return;
    }
    
    //Make sure one register request on one time
    if (resetPasswordReq != nil) 
        return;
    
    NSString *filePath = [[PathHelper documentDirectoryPathWithName:@"Acc"] 
                          stringByAppendingPathComponent:@"accounts.info"];
    
    NAAccount *acc = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    resetPasswordReq = [[RequestEngine alloc] initWithTarget:self action:@selector(resetPasswordRequestDidFinished:withObject:)];
    [resetPasswordReq resetAccountPassword:acc.userID oldPassword:self.oldPasswordTextfield.text newPassword:self.newerPasswordTextfield.text];
}

#pragma mark -
#pragma mark - Private methods

- (void) resetPasswordRequestDidFinished:(RequestEngine *) engine 
                              withObject:(id) object {
    
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dict = (NSDictionary *) object;
        [dict retain];
        
        /*
        if ([[dict objectForKey:@"status"] isEqualToString:@"1"]) {
            [NAError showAlertMessageWithTitle:@"" message:[dict objectForKey:@"msg"]];
        }else {
            [NAError showAlertMessageWithTitle:@"" message:[dict objectForKey:@"msg"]];
        }
        */
        
        NSString *msg = [dict objectForKey:@"msg"];
        // NSString *order_id = [result objectForKey:@"order_id"];
        
        
        ChecksEndViewController *vctrl = [[ChecksEndViewController alloc] init];
        vctrl.titleText = [dict objectForKey:@"top_title"];
        vctrl.msg = [msg retain];
        self.title = @"返回";
        [self.navigationController pushViewController:vctrl animated:YES];
        [vctrl release];
        [dict release];
    }
    else {
        [[NAError errorWithCode:NAError_REG_FAIL] showErrorMessage:nil];
    }
    
    NA_RELEASE_SAFELY(resetPasswordReq);
}



@end
